var $dataChart;

require(['grid', 'topBar', 'layout', 'date', 'jquery-ui'], function () {

    var postData;

    $('#top').topBar({
        left: [{
            type: 'filter-more',
            dateType:'custom',
            search: function () {
                if (getPostData()) {
                    $('#grid').grid('setGridParam', {
                        page: 1,
                        datatype: "json",
                        postData: postData
                    }).trigger("reloadGrid");
                }
            },
            item: [
                {
                    text: '时间快捷',
                    name1: 'beginTime',
                    name2: 'endTime',
                    type: 'dateLine'
                }
            ]
        }
        ],
        right: [
            {
                text: '导出',
                type: 'export-btn',
                authority: '',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                            exportFile('Excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                            exportFile('Pdf');
                        }
                    }
                ]
            },
            {
                text: '续航里程分析',
                type: 'btn',
                authority: '',
                click: function () {
                    dataChart('续航里程图示');
                }
            },
            {
                text: '电压电流分析',
                type: 'btn',
                authority: '',
                click: function () {
                    dataChart('电压电流图示');
                }
            }
        ]
    });

    $('body').layout();

    function exportFile(suffix) {
        if (getPostData()) {
            if ($('#grid').getGridParam("reccount") > 0) {
                var url = "/batteryStatus/exportBatteryStatus" + suffix + ".htm";
                $.exportFile(url, postData);
            }
            else {
                $.messager.alert("提示", "请先查询数据。", "info");
            }
        }
    }

    //弹出图表
    function dataChart(title) {
        var nodes = commonFun.getLeftTreeCheckedNodes("CAR");
        if (!nodes) {
            $.messager.alert("提示", "请选择查询车辆。", "info");
            return;
        }
        if (nodes.length > 1) {
            $.messager.alert("提示", "只能选择一辆车。", "info");
            return;
        }
        var data = $('#top').parent().getValues();
        var beginTime = data.beginTime;
        var endTime = data.endTime;
        if (beginTime.substr(0, 10) != endTime.substr(0, 10)) {
            $.messager.alert("提示", "只能分析一天内的数据,请重新选择日期。", "info");
            return;
        }
        if (!$dataChart) {
            $dataChart = $('<div>').dialog({
                url: './mainte/report/viewDataChart.htm',
                height: 550,
                width: 720,
                buttons: [
                    {
                        text: '关闭',
                        css: 'btn-cancel',
                        click: function () {
                            $(this).dialog('close');
                        }
                    }]
            });
        }
        data.carId = nodes[0];
        data.funcId = '01';
        if (title == '电压电流图示') {
            $dataChart.dialog('exec', 'voltageAnaly', data);
        }
        else {
            $dataChart.dialog('exec', 'enduranceMile', data);
        }
        $dataChart.dialog('setOption', 'title', title);
        $dataChart.dialog('open');
    }

    //获取左侧选择的节点信息
    function getPostData() {
        var rcx = commonFun.getTreeCheckNodeToArgs();
        if (rcx) {
            postData = $.extend(rcx, $("#top").parent().getValues())
        }
        return rcx;
    }

    $('#grid').grid({
        url: "/batteryStatus/searchBatteryStatus.htm",
        multiselect: false,
        colModel: [
            {name: "rcrdId", label: "id", key: true, hidden: true},
            {name: "carId", label: "carId", key: true, hidden: true},
            {name: "licPltNo", label: "车牌号", width: "70"},
            {name: "lineName", label: "运营线路", width: "70"},
            {name: "organName", label: "所属机构", width: "70"},
            {name: "rcrdTime", label: "采集时间", width: "120"},
            {name: "totalMile", label: "总里程(km)", align: "right", width: "100"},
            {name: "spdOfMotVhi", label: "车速(km/h)", align: "right", width: "80"},
            {name: "bteryMaterialType", label: "电池材料类型", width: "100"},
            {name: "bterySt", label: "电池状态", width: "80"},
            {name: "totalVtg", label: "总电压(V)", align: "right", width: "80"},
            {name: "totalEltCrt", label: "总电流(A)", align: "right", width: "80"},
            {name: "soc", label: "SOC值(%)", align: "right", width: "80"},
            {name: "durationMile", label: "续航里程(km)", align: "right", width: "90"},
            {name: "highestBteryVtg", label: "最高单体电压(V)", align: "right", width: "110"},
            {name: "highestVtgBoxno", label: "最高单体电压编号", align: "right", width: "115"},
            {name: "lowestBteryVtg", label: "最低单体电压(V)", align: "right", width: "110"},
            {name: "lowestVtgBoxno", label: "最低单体电压编号", align: "right", width: "115"},
            {name: "avrgBteryVtg", label: "平均电压(V)", align: "right", width: "90"},
            {name: "vtgDiff", label: "压差(V)", align: "right", width: "80"},
            {name: "highestTept", label: "最高单体温度(°C)", align: "right", width: "110"},
            {name: "highestTeptBoxno", label: "最高单体温度编号", align: "right", width: "115"},
            {name: "lowestTept", label: "最低单体温度(°C)", align: "right", width: "110"},
            {name: "lowestTeptBoxno", label: "最低单体温度编号", align: "right", width: "115"},
            {name: "avrgBteryTept", label: "平均温度(°C)", align: "right", width: "90"},
            {name: "teptDiff", label: "温差(°C)", align: "right", width: "80"},
            {name: "chargeDischarEltCrt", label: "充放电电流", align: "right", width: "80"},
            {name: "bteryBoxHighestTept", label: "电池箱最高温度", align: "right", width: "80"}]
    });

})
